import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-08-15
 * Time: 15:11
 */
//案例2:实现文件复制,也就是把文件中每个字节读取,写入另一个文件中
public class demo12 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入源文件路径");
        String srcPath = scanner.next();
        File srcFile = new File(srcPath);

        if (!srcFile.isFile()) {
            System.out.println("源文件不存在");
            return;
        }

        System.out.println("输入目标文件路径");
        String destPath = scanner.next();
        File destFile = new File(destPath);
        if (!destFile.getParentFile().isDirectory()) {
            System.out.println("目标文件的路径不存在");
            return;
        }

        //开始复制
        try (InputStream inputStream = new FileInputStream(srcFile);
             OutputStream outputStream = new FileOutputStream(destFile)) {
            while (true) {
                //读取源文件的内容
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if (n == -1) {
                    break;
                }
//                buffer写入outputStream
                outputStream.write(buffer, 0, n);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
